﻿using System;
using System.Collections.Generic;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Core;

namespace ZeroBug.EasyFlow.Step
{
    public class AccessVariableStep : IStep
    {
        public bool IsSingleRun { get; set; }
        public bool IgnoreChildren { get; set; }
        public string Icon { get; set; }
        public int ChildrenIndex { get; set; }
        public string VariableName { get; set; }
        public string VariableValue { get; set; }
        public Core.Step? NextStep { get; set; }
        public bool Compile(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;
            if (flow.GetFlowManager().VariableManager.GetVariable(VariableName) == null)
            {
                errInfo = $"变量{VariableName}不存在";
                return false;
            }
            return true;
        }

        public object GetForm()
        {
            throw new NotImplementedException();
        }

        public StepInfo GetInfo()
        {
            StepInfo info = new StepInfo();
            info.StepName = "AccessVariable";
            info.Group = StepGroup.Variable;
            info.ImagePath = "pack://application:,,,/ZeroBug.EasyFlow.Step;component/Images/条件.png";
            return info;
        }

        public void OnChildrenEnter()
        {
            throw new NotImplementedException();
        }

        public void OnChildrenLeave()
        {
            throw new NotImplementedException();
        }

        public bool Prepare(FlowBase flow, Core.Step step, out string errInfo)
        {
            throw new NotImplementedException();
        }

        public void RegisterVariables(FlowBase flow, Core.Step step)
        {
            Variable receivedVar = new Variable($"{step.Description}.Value", "-1", VariableType.Numeric);
            receivedVar.GetCurrentValues = () => new string[1] { index.ToString() };
            flow.GetFlowManager().VariableManager.AddVariable(receivedVar, VariableFrom.Local);
        }
        int index = -1;
        public StepResult Run(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;
            index = flow.GetFlowManager().VariableManager.GetVariable(VariableName).IndexOf(VariableValue);
            return StepResult.Success;
        }

        public string GetTip(Core.Step step)
        {
            return "";
        }

        public (SerializableDictionary<string, VariableExpression>, List<VariableJudge>) GetParametersAndConditions()
        {
            throw new NotImplementedException();
        }

        public void UnRegisterVariables(FlowBase flow, Core.Step step)
        {
        }
    }
}
